ucode: add error reporting to pcap_write
authorFelix Fietkau <[email protected]>
Tue, 22 Jul 2025 08:48:57 +0000 (10:48 +0200)
committerFelix Fietkau <[email protected]>
Tue, 22 Jul 2025 08:48:58 +0000 (10:48 +0200)
Return null on error, true if packets were written,
false if no data available.

Signed-off-by: Felix Fietkau <[email protected]>
lib-pcap.c
lib-ucode.c
udebug-pcap.h

index 634078999fb9c7c8537fb4c9b4e2bc8495e03125..329372239d9ab03cae41f8fa5272b09887a8eeaf 100644 (file)
@@ -252,10 +252,13 @@ int pcap_snapshot_packet_init(struct udebug *ctx, struct udebug_iter *it)
        return 0;
 }
 
-void pcap_block_write_file(FILE *f)
+bool pcap_block_write_file(FILE *f)
 {
-       fwrite(pcap_buf, pcap_hdr->len, 1, f);
+       if (fwrite(pcap_buf, pcap_hdr->len, 1, f) != 1)
+               return false;
+
        fflush(f);
+       return true;
 }
 
 void *pcap_block_get(size_t *len)
index 92a7dbb34f514c1241458bfc8bc9567406fedf01..2b1d57f0c12b82ba57d2efbfac2d4bfa7fc6c22a 100644 (file)
@@ -351,6 +351,7 @@ uc_udebug_pcap_write(uc_vm_t *vm, size_t nargs)
        size_t n = ucv_type(arg) == UC_ARRAY ? ucv_array_length(arg) : 1;
        struct udebug_snapshot **s;
        struct udebug_iter it;
+       bool ret = false;
 
        if (!p)
                return NULL;
@@ -359,10 +360,10 @@ uc_udebug_pcap_write(uc_vm_t *vm, size_t nargs)
        if (ucv_type(arg) == UC_ARRAY)
                for (size_t i = 0; i < n; i++) {
                        if ((s[i] = uc_get_snapshot(ucv_array_get(arg, i))) == NULL)
-                               return NULL;
+                               goto out;
        } else {
                if ((s[0] = uc_get_snapshot(arg)) == NULL)
-                       return NULL;
+                       goto out;
        }
 
        udebug_iter_start(&it, s, n);
@@ -374,16 +375,21 @@ uc_udebug_pcap_write(uc_vm_t *vm, size_t nargs)
                        if (pcap_interface_rbuf_init(&p->pcap, rb))
                                continue;
 
-                       pcap_block_write_file(p->f);
+                       if (!pcap_block_write_file(p->f))
+                               return NULL;
                }
 
                if (pcap_snapshot_packet_init(&u, &it))
                        continue;
 
-               pcap_block_write_file(p->f);
+               if (!pcap_block_write_file(p->f))
+                       return NULL;
+
+               ret = true;
        }
 
-       return NULL;
+out:
+       return ucv_boolean_new(ret);
 }
 
 static void
index 7c006b71107bf909ab9125f2d283fac2fc22e8b4..2ec83894ba13e1dbb954ec63182c1833eee8ec3f 100644 (file)
@@ -43,7 +43,7 @@ void pcap_packet_done(void);
 int pcap_interface_rbuf_init(struct pcap_context *p, struct udebug_remote_buf *rb);
 int pcap_snapshot_packet_init(struct udebug *ctx, struct udebug_iter *it);
 
-void pcap_block_write_file(FILE *f);
+bool pcap_block_write_file(FILE *f);
 void *pcap_block_get(size_t *len);
 
 #endif